PROJECT = "httpdemo"
VERSION = "1.0.0"

local sys = require "sys"

if wlan ~= nil then
    log.info("mac", wlan.get_mac())
    local ssid = "NodeMCU"
    local password = "xjau123456"
    wlan.connect(ssid, password)
end

sys.taskInit(
    function()
        -- 等待联网成功
        while true do
            while not socket.isReady() do
                log.info("net", "wait for network ready")
                sys.waitUntil("NET_READY", 1000)
            end
            log.info("main", "http loop")
            sys.wait(1000)
            http.get(
                "http://www.luatos.com/api/public/weather2?city=411325",
                nil,
                function(code, headers, body)
                    local data ={}
                    data = body--"'" .. body .. "'"
                    log.info("天气", code, data)
                    mydata = data--json.encode(data)
                    jsondata = json.decode(mydata)
                    log.info("天气", jsondata.live.humidity)
                end
            )
            -- data='{"city":"南阳市","live":{"weather":"霾","temperature":"-1","winddirection":"西北","windpower":"≤3","humidity":"64","reporttime":"2021-01-05 22:52:01"},"forecasts":[{"date":"2021-01-05","week":"2","dayweather":"多云","nightweather":"多云","daytemp":"4","nighttemp":"-5","daywind":"东北","nightwind":"东北","daypower":"4","nightpower":"4"},{"date":"2021-01-06","week":"3","dayweather":"多云","nightweather":"多云","daytemp":"4","nighttemp":"-7","daywind":"东北","nightwind":"东北","daypower":"4","nightpower":"4"},{"date":"2021-01-07","week":"4","dayweather":"多云","nightweather":"多云","daytemp":"-1","nighttemp":"-8","daywind":"南","nightwind":"南","daypower":"≤3","nightpower":"≤3"},{"date":"2021-01-08","week":"5","dayweather":"晴","nightweather":"多云","daytemp":"4","nighttemp":"-7","daywind":"西南","nightwind":"西南","daypower":"≤3","nightpower":"≤3"}]}'
            -- mydata = json.decode(data)
            -- log.info("http", mydata.live.humidity)
            sys.wait(3000)
        end
    end
)

-- 用户代码已结束---------------------------------------------
-- 结尾总是这一句
sys.run()